﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;

namespace Library
{
    public partial class UpdateBook : Form
    {
        Book oldBook;
        public Book NewBook { get; private set; }

        public UpdateBook(Book oldBook)
        {
            InitializeComponent();
            this.oldBook = oldBook;
        }
        private void UpdateBook_Load(object sender, EventArgs e)
        {
            ID_txbx.Text = oldBook.ID.ToString();
            Title_txbx.Text = oldBook.Name;
            Publisher_txbx.Text = oldBook.Publisher;
            PublishedDate_txbx.Text = oldBook.PublishedDate.ToString();
            ISBN_txbx.Text = oldBook.ISBN;
            AuthorName_txbx.Text = oldBook.AuthorName;
        }
        private void UpdateBook_btn_Click(object sender, EventArgs e)
        {
            if (Title_txbx.Text.Trim() == "" ||
            Publisher_txbx.Text.Trim() == "" ||
            PublishedDate_txbx.Text.Trim() == "" ||
            ISBN_txbx.Text.Trim() == "" ||
            AuthorName_txbx.Text.Trim() == "")
            {
                MessageBox.Show("Заполните все поля!");
                return;
            }

            if (Title_txbx.Text.Trim() == oldBook.Name &&
            Publisher_txbx.Text.Trim() == oldBook.Publisher &&
            DateTime.Parse(PublishedDate_txbx.Text.Trim()) == oldBook.PublishedDate &&
            ISBN_txbx.Text.Trim() == oldBook.ISBN &&
            AuthorName_txbx.Text.Trim() == oldBook.AuthorName)
            {
                MessageBox.Show("Информация о книге не была изменена.");
                return;
            }

            this.NewBook  = new Book();
            NewBook.ID = oldBook.ID;
            NewBook.Name= Title_txbx.Text.Trim();
            NewBook.Publisher = Publisher_txbx.Text.Trim();
            NewBook.PublishedDate = DateTime.Parse(PublishedDate_txbx.Text.Trim());
            NewBook.ISBN = ISBN_txbx.Text.Trim();
            NewBook.AuthorName = AuthorName_txbx.Text.Trim();

            DialogResult = DialogResult.OK;
        }

    }
}
